package com.zfy.cmsservice.controller;


import com.zfy.cmsservice.entity.Semesters;
import com.zfy.cmsservice.entity.Students;
import com.zfy.cmsservice.service.SemestersService;
import com.zfy.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学期表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-12-02
 */
@RestController
@RequestMapping("/cmsservice/semesters")
@Api(description = "学期管理-学期信息维护")
public class SemestersController {
    @Autowired SemestersService semestersService;
//    分页查询学期支持 根据学期名 时间检索
    @ApiOperation(value = "分页查询学期")
    @PostMapping("pageSemesters/{page}/{size}")
    public R pageSemesters(@PathVariable long page, @PathVariable long size,
                           @RequestParam(required = false) String name){
        return semestersService.pageSemesters(page,size,name);
    }

//    添加学期操作
    @ApiOperation(value = "添加学期")
    @PostMapping("addSemesters")
    public R addSemesters(@RequestBody Semesters semesters) {
        boolean flag = semestersService.save(semesters);
        if(flag) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

//    修改学期信息
    @ApiOperation(value = "修改学期信息")
    @PostMapping("updateSemesters")
    public R updateSemesters(@RequestBody Semesters semesters) {
        if (StringUtils.isEmpty(semesters)) {
            // 传参错误异常
            return R.httpError();
        }
        boolean flag = semestersService.updateById(semesters);
        if(flag) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

//   逻辑删除学期信息
    @ApiOperation(value = "逻辑删除学期信息")
    @PostMapping("removeSemesters/{id}")
    public R removeSemesters(@PathVariable String id) {
        boolean remove = semestersService.removeById(id);
        if(remove) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

//    根据id查询学期信息
    @ApiOperation(value = "根据id查询学期信息")
    @GetMapping("getSemesters/{id}")
    public R getSemesterById(@PathVariable String id) {
        Semesters byId = semestersService.getById(id);
        return R.ok().data("item", byId);
    }

    //查询所有学期
    @ApiOperation(value = "查询所有学期")
    @GetMapping("findAllSemesters")
    public R findAllSemesters() {
        List<Semesters> content = semestersService.findAllSemesters();
        return R.ok().data("content", content);
    }
}

